//
//  BusinessProfileCtrl.m
//  iWeddingDirectory
//
//  Created by duong2179 on 7/27/12.
//  Copyright (c) 2012 Home. All rights reserved.
//

#import "BusinessProfileCtrl.h"

#define BASE_BTN_TAG 100

@implementation BusinessProfileCtrl

@synthesize imvLargeImg, scvGallery, marImages, post, postType, contentView, txvDescription, scvMainView, mainView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    //load images here
    //calculate contentWidth
    contentWidth = 320.0f;
    contentHeight = 100.0f;
    
    mainView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 640.0f)];
    
    scvMainView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, self.view.frame.size.height)];
    [scvMainView setContentSize:CGSizeMake(320.0f, 640.0f)];
    [scvMainView setScrollEnabled:YES];
    [scvMainView setDelegate:self];
    [scvMainView setShowsHorizontalScrollIndicator:NO];
    [scvMainView setPagingEnabled:NO];
    [self.view addSubview:scvMainView];
    [scvMainView release];
    [scvMainView addSubview:mainView];
    
    //create imageview
    imvLargeImg = [[UIImageView alloc] initWithFrame:CGRectMake(10.0f, 10.0f, 300.0f, 160.0f)];
    [mainView addSubview:imvLargeImg];
    
    //create scrollview (gallery)
    scvGallery = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f, 180.0f, 320.0f, contentHeight)];
    CGSize contentSize = CGSizeMake(contentWidth, contentHeight);
    [scvGallery setContentSize:contentSize];
    [scvGallery setScrollEnabled:YES];
    [scvGallery setDelegate:self];
    [scvGallery setShowsHorizontalScrollIndicator:YES];
    [scvGallery setPagingEnabled:NO];
    [mainView addSubview:scvGallery];
    [scvGallery release];
    
    //create gallery
    contentView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, contentWidth, contentHeight)];
    [scvGallery addSubview:contentView];
    [contentView release];
    
    txvDescription = [[UIWebView alloc] initWithFrame:CGRectMake(10.0f, 290.0f, 300.0f, 200.0f)];
    [txvDescription setAutoresizingMask:UIViewContentModeScaleToFill];
    [mainView addSubview:txvDescription];
    [txvDescription release];
    
    if (postType == REMOTE_POST) {
        UIActivityIndicatorView *waitingIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        [waitingIndicator setTag:1000];
        [waitingIndicator setCenter:CGPointMake(160, 181)];
        [self.view addSubview:waitingIndicator];
        [waitingIndicator startAnimating];
        [waitingIndicator release];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onPostReceived:) name:kPostReceivedNotification object:nil];
        API *api = [API sharedInstance];
        [api getPostWithId:post.postId];
    }
    else {
        [self performSelector:@selector(loadThumbnail) withObject:nil afterDelay:0.05];
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc
{
    [imvLargeImg release];
    [scvGallery release];
    [marImages release];
    [contentView release];
    [txvDescription release];
    [scvMainView release];
    [mainView release];
}

- (void)onPostReceived:(NSNotification *)notification
{
    NSLog(@"load image here");
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kPostReceivedNotification object:nil];
    
    UIActivityIndicatorView *waitingIndicator = (UIActivityIndicatorView *)[self.view viewWithTag:1000];
    [waitingIndicator stopAnimating];
    
    NSDictionary *result = [notification userInfo];
    NSDictionary *bizInfo = [result objectForKey:@"business_info"];
    NSString *content = [bizInfo objectForKey:@"description"];
    NSString *thumbnail = [bizInfo objectForKey:@"display_image"];
    //post.title = title;
    post.content = content;
    //[txvDescription setText:content];

    [txvDescription loadHTMLString:content baseURL:nil];
    
    if (thumbnail != nil && ![thumbnail isEqual:[NSNull null]]) {
        post.thumbnail = thumbnail;
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:thumbnail]];
        UIImage *img = [UIImage imageWithData:data];
        
        //dieu chinh frame cua imageview cho vua anh
        CGRect frame;
        CGSize size = img.size;
        if(size.width > 300.0f)
            size.width = 300.0f;
        if(size.height > 160.0f)
            size.height = 160.0f;
        frame.size = size;
        frame.origin = CGPointMake(160.0f - size.width / 2, 10.0f);
        [imvLargeImg setFrame:frame];
        imvLargeImg.image = img;
    }
    
    //dieu chinh frame cua scroll view theo anh
    CGRect frame1;
    float yOff1 = 10.0f + imvLargeImg.frame.size.height + 10.0f;
    frame1 = [scvGallery frame];
    frame1.origin.y = yOff1;
    [scvGallery setFrame:frame1];
    
    //dieu chinh frame cua description theo anh
    CGRect frame2;
    float yOff2 = scvGallery.frame.origin.y + scvGallery.frame.size.height + 10.0f;
    frame2 = [txvDescription frame];
    frame2.origin.y = yOff2;
    [txvDescription setFrame:frame2];
    
    NSMutableArray *gallery = [bizInfo objectForKey:@"gallery"];
    //NSMutableArray *gallery = [NSMutableArray arrayWithObjects:@"flower01.png", @"flower02.png", @"flower03.png", @"flower04.png", @"flower05.png", @"flower06.png", @"flower07.png", @"flower08.png", @"flower09.png", nil];
    UIButton *btnGallery;
    marImages = [[NSMutableArray alloc] init];
    int count = [gallery count];
    float width;
    if(count % 4 == 0)
        width = 320.0f * (count/4);
    else
        width = 320.0f * (count/4 + 1);
    
    [scvGallery setContentSize:CGSizeMake(width, contentHeight)];
    CGRect frame = [contentView frame];
    frame.size = CGSizeMake(width, contentHeight);
    [contentView setFrame:frame];
    
    for(int i=0; i<[gallery count]; i++)
    {
        NSLog(@"%@", [gallery objectAtIndex:i]);
        btnGallery = [[UIButton alloc] initWithFrame:CGRectMake(5.0f + i*80.0f, 0.0f, 70.0f, contentHeight)];
        [btnGallery setTag:(BASE_BTN_TAG + i)];
        [btnGallery addTarget:self action:@selector(btnGalleryClicked:) forControlEvents:UIControlEventTouchDown];
    
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[gallery objectAtIndex:i]]];
        UIImage *img = [UIImage imageWithData:data];
        //UIImage *img = [UIImage imageNamed:[gallery objectAtIndex:i]];
        if(img==nil)
            continue;
        
        [marImages addObject:img];
        [btnGallery setImage:img forState:UIControlStateNormal];
        [contentView addSubview:btnGallery];
        [btnGallery release];
    }
    
}

- (void)loadThumbnail
{
    NSLog(@"load thumbnail");
}

- (void)btnGalleryClicked:(id)sender
{
    int idx = [sender tag] - BASE_BTN_TAG;
    UIImage *img = [marImages objectAtIndex:idx];
    CGRect frame;
    CGSize size = img.size;
    if(size.width > 300.0f)
        size.width = 300.0f;
    if(size.height > 160.0f)
        size.height = 160.0f;
    frame.size = size;
    frame.origin = CGPointMake(160.0f - size.width / 2, 10.0f);
    [imvLargeImg setFrame:frame];
    imvLargeImg.image = img;
    
    //dieu chinh lai frame cua scroll
    CGRect frame1;
    float yOff1 = 10.0f + imvLargeImg.frame.size.height + 10.0f;
    frame1 = [scvGallery frame];
    frame1.origin.y = yOff1;
    [scvGallery setFrame:frame1];
    
    //dieu chinh frame cua description theo anh
    CGRect frame2;
    float yOff2 = scvGallery.frame.origin.y + scvGallery.frame.size.height + 10.0f;
    frame2 = [txvDescription frame];
    frame2.origin.y = yOff2;
    [txvDescription setFrame:frame2];
}

@end
